Loading packages
pacman::p_load(leaflet, glue, dplyr, sf, tmap, tmaptools, tidycensus, ggmap, htmltools, htmlwidgets, tidyverse)
pacman::p_load_gh(c("walkerke/tigris", "bhaskarvk/leaflet.extras"))
Downloading shapefile of NC counties
options(tigris_use_cache = TRUE)
nc_counties <- counties("NC")
Loading our health department data
library(gsheet)
AllHealthDepartments <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1Tp6E7I3lguVumOMuFyoJJQ5naTroHmkeXqnXXqvq1cI/edit#gid=0")
library(gsheet)
CompiledLHDExpenditures <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1Zc10pam92Y1218F90eXn9ri7-a4GQI1vhzING33nNSI/edit#gid=0")
Renaming column so name is consistent with health data column name so data frames can be joined
nc_counties<- dplyr::rename(nc_counties, county_name = NAME)
Joining NC counties shapefile with our health department data
library(raster)
## Loading required package: sp
##
## Attaching package: 'raster'
## The following object is masked from 'package:tidyr':
##
## extract
## The following object is masked from 'package:dplyr':
##
## select
## The following object is masked from 'package:glue':
##
## trim
# merge on common variable, here called 'key'
county_map <- merge(nc_counties, AllHealthDepartments, by="county_name")
#Making a 2009 expenditure map
AllHealthDepartments2009 <- AllHealthDepartments %>%
filter(year== "2009")
county_map2009 <- merge(nc_counties, AllHealthDepartments2009, by="county_name")
#2009 Expenditure Map Title isnt displaying Need outlines of other counties
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(county_map2009) +
tm_polygons("expenditures",
breaks= c(0, 5000000, 10000000, 15000000, 20000000, 25000000, 30000000,80000000),palette="YlGnBu", title="Fiscal Year 2009 Expenditures") +
tm_borders() +
tm_layout(title = "Health Department Fiscal Year 2009 Spending") +
tm_legend(legend.position = c("left", "bottom"))
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
#Making a 2019 expenditure map
AllHealthDepartments2019 <- AllHealthDepartments %>%
filter(year== "2019")
county_map2019 <- merge(nc_counties, AllHealthDepartments2019, by="county_name")
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(county_map2019) +
tm_polygons("expenditures",
breaks= c(0, 5000000, 10000000, 15000000, 20000000, 25000000, 30000000,80000000),palette="YlGnBu", title="Fiscal Year 2019 Expenditures") +
tm_borders() +
tm_layout(title = "Health Department Fiscal Year 2019 Spending") +
tm_legend(legend.position = c("left", "bottom"))
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
#Making 2019 Per Capita Spending Map Legend needs to have dollar signs added Hover pop ups need more detail
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(county_map2019) +
tm_polygons("per_capita_spending", palette="YlGnBu", title="Fiscal Year 2019 Per Capita Spending") +
tm_borders() +
tm_layout(title = "Health Department Fiscal Year 2019 Per Capita Spending") +
tm_legend(legend.position = c("left", "bottom"))
## legend.postion is used for plot mode. Use view.legend.position in tm_view to set the legend position in view mode.
## Warning: One tm layer group has duplicated layer types, which are omitted. To
## draw multiple layers of the same type, use multiple layer groups (i.e. specify
## tm_shape prior to each of them).
MAKE A MAP SHOWING EXPENDITURE PERCENT CHANGE
Loading in the code of data frame with percent change column
ExpenditurePctChange <- CompiledLHDExpenditures %>%
filter(year %in% c('2009', '2019')) %>%
pivot_wider(id_cols = -c(per_capita_spending,population), names_from = year, values_from = expenditures) %>%
mutate(expenditure_change= (`2019` - `2009`),
pct_change = 100 * ((`2019` - `2009`)) / `2009`) %>%
arrange(desc(pct_change))
Merging shapefile and data
# merge on common variable, here called 'key'
pct_map <- merge(nc_counties, ExpenditurePctChange, by="county_name")
Creating map showing percent change **Need help refining legend position and legend labeling–want them to be percents
tm_shape(pct_map) +
tm_fill("pct_change",title="Percent Change in Health Department Expenditures from 2009-2019",palette="YlGnBu") +
tm_borders() +
tm_layout(title = "Custom Breaks Map", title.position = c("right","bottom"))